
package xml;

import com.sun.org.apache.xerces.internal.impl.xpath.XPath;
import java.io.File;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import model.Board;
import model.Submarine;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Karnit Genchay
 */
public class XmlParser {
    public Document doc;
    private HashMap<String,Integer>  submarineTypes;
    private Submarine submarines;
    
   public XmlParser(){
        try {
           
  File file = new File("c:\\battleship.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
   doc = db.parse(file);

}   catch (Exception e) {
    throw new RuntimeException();
  }
        buildSubmarinesMap();
   }
  
   
public static void main(String[] args) 
    {
      XmlParser xm = new XmlParser();
   Board b[] =   xm.buildPlayersBoard();
   
        for (int i = 0; i < 4; i++) {
            b[i].print();
        }
     }
    public int getSubmarineTypesSize(){return submarineTypes.size();}


   public int getNumOf(String name){
       return (doc.getElementsByTagName(name)).getLength();
   }
    public String getAttribute(String tag,String attribute,int i){
        NodeList nl = doc.getElementsByTagName(tag);

    return ( (Element)nl.item(i)).getAttribute(attribute);
    } 
    public Board[]  buildPlayersBoard(){
         int numOfPlayers = doc.getElementsByTagName("player").getLength();
         NodeList data = doc.getElementsByTagName("location");
         Board [] boards = new Board [numOfPlayers];
         String submarineName,position,direction;
         Element el ;
         Submarine newSubmarine;
               
         int submarineSize;
         int playerNum=0;

            for (int i =0; i < data.getLength(); i++) {
                el = (Element)data.item(i);
                position = el.getAttribute("position");
                direction = el.getAttribute("direction");
          
           if(i%submarineTypes.size()==0){
               playerNum = i/submarineTypes.size();
               boards[playerNum] = new Board(submarineTypes.size());
           }
            submarineName =  el.getAttribute("name");
            submarineSize = submarineTypes.get(submarineName);    
            newSubmarine = new Submarine(submarineSize, submarineName, position, direction);
            boards[playerNum].setNewSubmarine(i%submarineTypes.size(),newSubmarine);
            }
            return  boards;
     
    }
    

//    public Player[] getPlayers(){
//    NodeList nl = doc.getElementsByTagName("player");
//    Player[] players = new Player[nl.getLength()];
//    for (int i = 0; i <nl.getLength(); i++) {
//
//               Element el = (Element)nl.item(i);
//               
//               //System.out.println(el.getAttribute("name"));
//               
//               players[i] = new Player(el.getAttribute("name"),el.getAttribute("type"));
//               System.out.println( players[i]);
//                //System.out.println(map.item(0));
//            }
//    return players;
//    } 

    
    private void buildSubmarinesMap(){
         NodeList nl = doc.getElementsByTagName("boat");
         submarineTypes = new HashMap<String, Integer>();
           Element el;
           
         for (int i = 0; i <nl.getLength(); i++) {
                el = (Element)nl.item(i);
               submarineTypes.put(el.getAttribute("name"), Integer.parseInt(el.getAttribute("size")));    
            }
//         System.out.println(submarineTypes);
    }
    public int submarinesMapSize (){    return submarineTypes.size();}




  
  
}
 
  
